package exam.tool;

import java.sql.*;

public class ConnectionTool {

    /**
     * 按照默认情况获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getDefaultConn() throws SQLException {
        return DriverManager.getConnection(
                Const.URL_TEST,
                Const.USERNAME,
                Const.PASSWORD
        );
    }

    /**
     * 指定数据库获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getDefaultConn(String database) throws SQLException {
        return DriverManager.getConnection(
                Const.URL+database,
                Const.USERNAME,
                Const.PASSWORD
        );
    }

    public static void closed(Connection conn, PreparedStatement ps){
        closed(conn,ps,null);
    }


    public static void closed(Connection conn, PreparedStatement ps, ResultSet rs){
        try {
            if(rs!=null){
                rs.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(conn!=null){
                conn.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
